<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="机构名称" prop="institutionName">
        <el-input v-model.trim="form.institutionName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="机构地址" prop="institutionalAddress">
        <el-input v-model.trim="form.institutionalAddress" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input v-model.trim="form.remark" autocomplete="off"></el-input>
      </el-form-item>
<!--      <el-form-item label="上级机构" prop="superiorOgi">-->
<!--        <el-input v-model.trim="form.superiorOgi" autocomplete="off"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="上级机构" prop="superiorOgi">
        <el-select
          clearable
          v-model="organ"



          style="width: 100%"
          placeholder="上级机构">
          <el-option
            v-for="item in organs"
            :key="item.institutionNumber"
            :label="item.institutionName"
            :value="item.institutionNumber">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from "@/api/table";
  import { searchchild } from "@/api/user";
  export default {
    name: "TableEdit",
    data() {
      return {
          organ:"",
          organs:{

          },

        form: {
            institutionName: " ",
            institutionalAddress: "",
            remark:"",
            superiorOgi:"",
        },
        rules: {
            institutionName: [{ required: true, trigger: "blur", message: "请输入机构名称" }],
            institutionalAddress: [{ required: true, trigger: "blur", message: "请输入机构地址" }],
            remark:[{  trigger: "blur", message: "备注信息" }],
            superiorOgi:[{  trigger: "blur", message: "上级机构" }]
        },
        title: "",
        dialogFormVisible: false,
      };
    },
    created() {
        let  institutionNumber= localStorage.getItem("institutionNumber");
        searchchild({institutionNumber}).then(res =>{
            console.log(res.data)
            this.organs=res.data


        })
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = "添加";
        } else {
          this.title = "编辑";
          this.form = Object.assign({}, row);
        }
        this.dialogFormVisible = true;
      },
      close() {
        this.$refs["form"].resetFields();
        this.form = this.$options.data().form;
        this.dialogFormVisible = false;
        this.$emit("fetchData");
      },
      save() {
          this.form.superiorOgi=this.organ
        this.$refs["form"].validate(async (valid) => {
          if (valid) {
              console.log(this.form)
            const data = await doEdit(this.form);

            this.$emit("fetchData");
            this.$baseMessage("添加成功");
            let  institutionNumber= localStorage.getItem("institutionNumber");
              searchchild({institutionNumber}).then(res =>{
                  console.log(res.data)
                  localStorage.setItem('childinstitutionNumber',JSON.stringify(res.data));

              })
            this.dialogFormVisible = false

          } else {
            return false;
          }
        });
      },
    },
  };
</script>
